<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList"
        :search-params="searchParams">
    </SearchForm>

    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="tableData"
        :height="tableHeight"
        border
        style="width: 100%">
        <el-table-column
          prop="path_name"
          label="学习路径"
          min-width="200">
          <template slot-scope="scope">
            <el-link 
              type="primary" 
              :underline="false"
              @click="onContinue(scope.row)">
              {{ scope.row.path_name }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="resource_name"
          label="课程名称"
          min-width="200">
          <template slot-scope="scope">
            <el-link 
              type="primary" 
              :underline="false"
              @click="onContinue(scope.row)">
              {{ scope.row.resource_name }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="domain"
          label="专业领域"
          width="120">
          <template slot-scope="scope">
            {{ getDomainName(scope.row.domain) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="progress"
          label="学习进度"
          width="200">
          <template slot-scope="scope">
            <el-progress 
              :percentage="scope.row.progress"
              :color="getProgressColor(scope.row.progress)"/>
          </template>
        </el-table-column>
        <el-table-column
          prop="study_time"
          label="学习时长(小时)"
          width="120">
        </el-table-column>
        <el-table-column
          label="知识点完成"
          width="120">
          <template slot-scope="scope">
            {{ scope.row.completed_nodes }}/{{ scope.row.total_nodes }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
          fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="onContinue(scope.row)">继续学习</el-button>
            <el-button
              size="mini"
              type="text"
              @click="onDetail(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          background
          :current-page="pageNo"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalSize"
          @size-change="changePageSize"
          @current-change="changePageNo"
        />
      </div>
    </div>
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";

export default {
  name: "LearningRecordIndex",
  mixins: [crud],
  data() {
    return {
      request: http.learning,
      searchList: [
        {
          label: '学习路径',
          prop: 'path_name',
          name: 'input',
          clearable: true
        },
        {
          label: '课程名称',
          prop: 'resource_name',
          name: 'input',
          clearable: true
        },
        {
          label: '专业领域',
          prop: 'domain',
          name: 'select',
          clearable: true,
          options: [
            { label: '铁道工程', value: 1 },
            { label: '信号控制', value: 2 },
            { label: '机车车辆', value: 3 }
          ]
        },
        {
          label: '学习状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            { label: '学习中', value: 1 },
            { label: '已完成', value: 2 },
            { label: '已暂停', value: 3 }
          ]
        }
      ],
      searchParams: {
        path_name: '',
        resource_name: '',
        domain: '',
        status: ''
      },
      defaultParams: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getDomainName(domain) {
      const domains = {
        1: '铁道工程',
        2: '信号控制',
        3: '机车车辆'
      }
      return domains[domain] || '-'
    },
    getProgressColor(progress) {
      if (progress >= 80) {
        return '#67C23A'
      } else if (progress >= 50) {
        return '#E6A23C'
      } else {
        return '#F56C6C'
      }
    },
    onContinue(row) {
      this.$router.push({
        name: 'LearningDetail',
        params: { id: row.resource_id }
      })
    },
    onDetail(row) {
      this.$router.push({
        name: 'learning-record-detail',
        params: { id: row.id }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.table-container {
  margin-top: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.el-progress {
  margin: 0;
}

.el-link {
  font-size: 14px;
  
  &:hover {
    color: #409EFF;
  }
}
</style> 